﻿@model SimpleTaskManager.WebUI.Models.SearchResultModel
@using SimpleTaskManager.WebUI.HtmlHelpers;


@{
    ViewBag.Title = "Search Result";
}

<h2>Search Result</h2>
@if (!ViewBag.Found)
{
    <h2>@ViewBag.Message</h2>
}
else
{
    if (Model.Users.Count > 0)
    {
        <h3>Users</h3>
        <table class="tablesorter" id="foundUsers">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Status</th>
                <th>Address</th>
                <th>E-mail</th>
                <th>Phone</th>
                @if (ViewBag.ShowEdit)
                { <th>Action</th> }
            </tr>
        </thead>
        <tbody>
        @foreach (var user in Model.Users)
        { 
            <tr>
                <td>@Html.ActionLink(user.FirstName + " " + user.LastName, "Details", "User", new { id = user.UserID }, null)</td>
                <td>@Html.DisplayFor(item => user.Position)</td>
                <td>@Html.DisplayFor(item => user.Status)</td>
                <td>@user.Address <br /> @user.ZIP @user.City , @user.Country</td>
                <td>@Html.DisplayFor(item => user.Email)</td>
                <td>@Html.DisplayFor(item => user.Phone)</td>
                @if (ViewBag.ShowEdit)
                { <td>@Html.ActionLink("Edit", "Edit", "User", new { id = user.UserID }, null) |
                                             @Html.ActionLink("Delete", "Delete", "User", new { id = user.UserID }, null)
                                         </td> }
            </tr>
        }
        </tbody>
        </table>
    }

    if (Model.Projects.Count > 0)
    {
        <h3>Projects</h3>
        <table class="tablesorter" id="foundProjects">
            <thead>
            <tr>
                <th>Name</th>
                <th>Deadline</th>
                <th>Leader</th>
                <th>Group</th>
                <th>Status</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var p in Model.Projects)
            {
                var task = p.MainTask;
                <tr>
                    <td>@Html.ActionLink(task.Name, "Details", "Task", new { id = task.TaskID }, null)</td>
                    <td>@task.EndDate.ToShortDateString()</td>
                    <td>@(task.Leader.LastName + ", " + task.Leader.FirstName)</td>
                    <td>@(task.Group.Count.ToString() + "/" + task.GroupSize.ToString())</td>
                    <td>@task.Status</td>
                </tr>
            }
            </tbody>
        </table>
    }

    if (Model.Tasks.Count > 0)
    {
        <h3>Tasks</h3>
        <table class="tablesorter" id="foundTasks">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Requires</th>
                    <th>Status</th>
                    <th>Progress</th>
                    <th>Deadline</th>
                    <th>Rating</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var t in Model.Tasks)
                {
                    <tr>
                        <td>@Html.ActionLink(t.Name, "Details", "Task", new { id = t.TaskID }, null)</td>
                        <td>@{
                    bool isFirst = true;
                    foreach (var reqTask in t.Requirements)
                    {
                        if (isFirst)
                        {
                            isFirst = !isFirst;
                        }
                        else
                        {
                                    <span>, </span>
                        }
                                @Html.ActionLink(reqTask.Name, "Details", "Task", new { id = reqTask.TaskID }, null);
                    }
                        }</td>
                        <td>@t.Status</td>
                        <td>@t.Progress.ToPercentage()</td>
                        <td>@t.EndDate.ToShortDateString()</td>
                        <td>@t.Rating</td>
                    </tr>
                }
            </tbody>
        </table>
    }

    if (Model.Reports.Count > 0)
    {
        <h3>Reports</h3>
        <table class="tablesorter" id="foundReports">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Uploaded By</th>
                    <th>Uploaded At</th>
                    <th>Approved</th>
                </tr>
            </thead>
            <tbody>
                @foreach (SimpleTaskManager.Domain.Entities.ReportHeader report in Model.Reports)
                {
                    <tr>
                        <td>@Html.ActionLink(report.Name, "ReportDetails", "Task",
                            new { taskId = report.Task.TaskID, reportId = report.ReportHeaderID }, null)</td>
                        <td>@(report.CreatedBy.FirstName + " " + report.CreatedBy.LastName)</td>
                        <td>@report.CreationDate.ToString("yyyy-MM-dd HH:mm:ss")</td>
                        <td>@(report.IsApproved
                        ? "yes (by " + report.ApprovedBy.FirstName + " " + report.ApprovedBy.LastName + ")"
                        : "no")</td>
                    </tr>
                }
            </tbody>
     </table>
    }
    <script type="text/javascript">
        $(document).ready(function () {
            $("#foundUsers").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });
            $("#foundProjects").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });
            $("#foundTasks").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });
            $("#foundReports").dataTable({ bJQueryUI: true, sPaginationType: "full_numbers" });
        }
);
</script>
}